package com.truyen.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.googlecode.ehcache.annotations.Cacheable;
import com.truyen.persistence.dao.AnhDepDao;
import com.truyen.persistence.entity.AnhDep;
import com.truyen.persistence.entity.ContentAnhDep;
import com.truyen.persistence.entity.LoaiAnhDep;
import com.truyen.service.AnhDepService;
import com.truyen.service.ContentAnhDepService;
import com.truyen.utils.PagingInfo;


@Service
public class AnhDepServiceImpl implements AnhDepService {
	
	@Autowired
	private AnhDepDao anhDepDao;
	
	@Autowired
	private ContentAnhDepService contentAnhDepService;

	@Override
	public void save(AnhDep entity) {
		anhDepDao.save(entity);
	}

	@Override
	public AnhDep findById(Long id) {
		return anhDepDao.findById(id);
	}

	@Override
	public List<AnhDep> getAll() {
		return anhDepDao.findAll();
	}

	@Override
	public AnhDep findByProperty(String column, Object value) {
		List<AnhDep> lst = anhDepDao.findByProperty(column, value);
		return (lst.size()>0)?lst.get(0):null;
	}

	@Override
	public List<AnhDep> getAllLst(String kwSearch, String sort, String sortBy,
			PagingInfo pInfo) {
		return anhDepDao.getAllLst(kwSearch,sort,sortBy,pInfo);
	}

	@Override
	public List<AnhDep> getAllWithIsGot(boolean isGot) {
		return anhDepDao.getAllWithIsGot(isGot);
	}

	@Override
	public void updateIsGot(long id, boolean isGot) {
		try {
			AnhDep anhDep = findById(id);
			if(anhDep != null){
				anhDep.setGot(isGot);
				anhDepDao.merge(anhDep);
			}
		} catch (Exception e) {
			// TODO: handle exception
		}
	}

	@Override
	public List<AnhDep> getLstAnhDep(PagingInfo pInfo, boolean isGot,
			String orderBy) {
		return anhDepDao.getLstAnhDep(pInfo,isGot,orderBy);
	}

	@Override
	public void setHot(Long id) {
		try {
			AnhDep anhDep = findById(id);
			if(anhDep != null){
				boolean flag = anhDep.getIsHot();
				if(flag){
					flag = false;
				}else{
					flag = true;
				}
				anhDep.setIsHot(flag);
				anhDepDao.merge(anhDep);
			}
		} catch (Exception e) {
			// TODO: handle exception
		}
	}

	@Override
	public void setIsDelete(Long id) {
		try {
			AnhDep anhDep = findById(id);
			if(anhDep != null){
				boolean flag = anhDep.getIsDelete();
				if(flag){
					flag = false;
				}else{
					flag = true;
				}
				anhDep.setIsDelete(flag);
				anhDepDao.merge(anhDep);
			}
		} catch (Exception e) {
			// TODO: handle exception
		}
	}

	@Override
	@Transactional
	public void delete(Long id) {
		try{
			AnhDep entity = findById(id);
			List<ContentAnhDep> lstContentAnhDep = entity.getContentAnhDeps();
			if(lstContentAnhDep != null){
				int sizeList = lstContentAnhDep.size();
				for(int i=0;i<sizeList;i++){
					ContentAnhDep contentAnhDep = lstContentAnhDep.get(i);
					contentAnhDepService.delete(contentAnhDep.getId());
				}
			}
			anhDepDao.delete(entity);
		}catch (Exception e) {
			// TODO: handle exception
		}
	}

	@Override
	@Cacheable(cacheName="anhDepByCategory")
	public List<AnhDep> getLstByType(LoaiAnhDep loaiAnhDep, int maxLimit, String orderBy, boolean isGot) {
		return anhDepDao.getLstByType(loaiAnhDep,maxLimit,orderBy,isGot);
	}

	@Override
	@Cacheable(cacheName="anhDepByCategoryWithPaging")
	public List<AnhDep> getLstByType(LoaiAnhDep loaiAnhDep, PagingInfo pInfo,
			String orderBy, boolean isGot) {
		return anhDepDao.getLstByType(loaiAnhDep,pInfo,orderBy,isGot);
	}

	@Override
	public List<AnhDep> getListRandom(LoaiAnhDep loaiAnhDep, int maxLimit,
			boolean isGot) {
		return anhDepDao.getListRandom(loaiAnhDep,maxLimit,isGot);
	}

	@Override
	public List<Object> getObjectByNameUnsigned(String kwSearch, int max) {
		return anhDepDao.getObjectByNameUnsigned(kwSearch,max);
	}

	@Override
	public void updateViews(Long idAnhDep, long views) {
		AnhDep anhDep = findById(idAnhDep);
		anhDep.setViews(views);
		anhDepDao.merge(anhDep);
	}

	@Override
	public List<AnhDep> getLstByPropertyWithPaging(String column,
			String kwSearch, String orderBy, PagingInfo pInfo, boolean isGot) {
		return anhDepDao.getLstByPropertyWithPaging(column,kwSearch,orderBy,pInfo,isGot);
	}


}
